//
//  XS_OrderItemCell.swift
//  XueDao
//
//  Created by Mac on 2020/12/21.
//

import UIKit

protocol XS_OrderItemCellDelegate {
    func orderItemCell(orderItemCell : XS_OrderItemCell, didClickCancelBtn:UIButton)
    func orderItemCell(orderItemCell : XS_OrderItemCell, didClickBuyBtn:UIButton)
    func orderItemCell(orderItemCell : XS_OrderItemCell, didClickLeftBtn:UIButton)
    func orderItemCell(orderItemCell : XS_OrderItemCell, didClickRightBtn:UIButton)
}

class XS_OrderItemCell: UICollectionViewCell {
    
    var delegate : XS_OrderItemCellDelegate?

    @IBOutlet weak var cancelBtn: UIButton!
    @IBOutlet weak var statusBtn: QMUIButton!
    @IBOutlet weak var timeLabel: UILabel!
    @IBOutlet weak var imgView: UIImageView!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var contentLabel: UILabel!
    @IBOutlet weak var priceLabel: UILabel!
    
    @IBOutlet weak var buyBtn: UIButton!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        cancelBtn.layer.borderWidth = 1
        cancelBtn.layer.borderColor = UIColor.qmui_color(withHexString: "#AAB1B7")?.cgColor
        statusBtn.imagePosition = .left
        statusBtn.spacingBetweenImageAndTitle = 4
        
        imgView.layer.masksToBounds = true
        imgView.layer.cornerRadius = .scale * 4
        imgView.contentMode = .scaleAspectFill
    }
    
    var _model : XS_OrderItemModel!
    public var model : XS_OrderItemModel!{
        set{
            _model = newValue
            //设置数据
            statusBtn.isSelected = newValue.state == "payment"
            statusBtn.isEnabled = newValue.state.contains("payment")
            timeLabel.text = newValue.orderTime
            imgView.sd_setImage(with: URL.init(string: newValue.curriculumImgUrl), completed: nil)
            titleLabel.text = newValue.curriculumName
            contentLabel.text = newValue.curriculumDesc
            priceLabel.text = "￥"+newValue.payMoney
            if newValue.payType == "wechat" || newValue.payType == "aliapy" {
                cancelBtn.isHidden = newValue.state == "payment" || newValue.state == "cancel"
                buyBtn.isHidden = newValue.state == "payment" || newValue.state == "cancel"
            }else{
                cancelBtn.isHidden = true
                buyBtn.isHidden = true
            }
            contentLabel.text = model.purchaseNumber + "人已加入学习"
        }
        get{
            return _model
        }
    }

    @IBAction func leftBtnAction(_ sender: UIButton) {
        if delegate != nil {
            delegate?.orderItemCell(orderItemCell: self, didClickLeftBtn: sender)
        }
    }
    
    @IBAction func rightBtnAction(_ sender: UIButton) {
        if delegate != nil {
            delegate?.orderItemCell(orderItemCell: self, didClickRightBtn: sender)
        }
    }
    
    @IBAction func cancelBtnAction(_ sender: UIButton) {
        if delegate != nil {
            delegate?.orderItemCell(orderItemCell: self, didClickCancelBtn: sender)
        }
    }
    
    @IBAction func buyBtnAction(_ sender: UIButton) {
        if delegate != nil {
            delegate?.orderItemCell(orderItemCell: self, didClickBuyBtn: sender)
        }
    }
}
